package com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.repository;

import com.css.fxfzfxqh.modules.buildingKilometerGrid.model.entity.FxqhBuildingGridRuralMixed;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.entity.FxqhAssessReportTemplate;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class FxqhAssessReportTemplateRepository {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(FxqhAssessReportTemplate fxqhAssessReportTemplate) {
        SqlParameterSource source = new BeanPropertySqlParameterSource(fxqhAssessReportTemplate);
        String sql = "INSERT INTO fxqh_assess_report_template(id, code, name, remark, file_id, del_flag, create_user, create_time, update_user, update_time) values(:id, :code, :name, :remark, :fileId, :delFlag, :createUser, :createTime, :updateUser, :updateTime)";
        namedParameterJdbcTemplate.update(sql, source);
    }

    public String getLatestTemplateCode() {
        try {
            return jdbcTemplate.queryForObject("select code from fxqh_assess_report_template where del_flag='0' order by code desc limit 1 offset 0", String.class);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public Integer countByCode(String code) {
        return jdbcTemplate.queryForObject("select count(*) from fxqh_assess_report_template where del_flag='0' and code='" + code + "'", Integer.class);
    }

    public List<FxqhAssessReportTemplate> list(String code, String name) {
        String sql = "select * from fxqh_assess_report_template where del_flag ='0' ";
        if (StringUtils.isNotBlank(code)) {
            sql += " and code like'%" + code + "'% ";
        }
        if (StringUtils.isNotBlank(name)) {
            sql += " and name like'%" + name + "'% ";
        }
        sql += " order by create_time desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FxqhAssessReportTemplate.class));
    }

    public void delete(String id, String uid, Date updateTime) {
        String sql = "update fxqh_assess_report_template set del_flag = '1', update_user = '" + uid + "', update_time = '" + updateTime + "' where id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    public void upload(String templateId, String fileId, String uid, Date updateTime) {
        String sql = "update fxqh_assess_report_template set file_id = '" + fileId + "', update_user = '" + uid + "', update_time = '" + updateTime + "' where id = '" + templateId + "'";
        jdbcTemplate.update(sql);
    }

    public FxqhAssessReportTemplate getById(String id) {
        String sql = "select * from fxqh_assess_report_template where id = '" + id + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(FxqhAssessReportTemplate.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public void update(FxqhAssessReportTemplate fxqhAssessReportTemplate) {
        String sql = "update fxqh_assess_report_template set del_flag = '0', ";
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getCode())) {
            sql += " code = '" + fxqhAssessReportTemplate.getCode() + "', ";
        }
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getName())) {
            sql += " name = '" + fxqhAssessReportTemplate.getName() + "', ";
        }
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getRemark())) {
            sql += " remark = '" + fxqhAssessReportTemplate.getRemark() + "', ";
        }
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getFileId())) {
            sql += " file_id = '" + fxqhAssessReportTemplate.getFileId() + "', ";
        }
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getCreateTime())) {
            sql += " create_user = '" + fxqhAssessReportTemplate.getCreateUser() + "',";
        }
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getCreateTime())) {
            sql += " create_time = '" + fxqhAssessReportTemplate.getCreateTime() + "', ";
        }
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getUpdateUser())) {
            sql += " update_user = '" + fxqhAssessReportTemplate.getUpdateUser() + "',";
        }
        if (ObjectUtils.isNotEmpty(fxqhAssessReportTemplate.getUpdateTime())) {
            sql += " update_time = '" + fxqhAssessReportTemplate.getUpdateTime() + "', ";
        }
        sql = StringUtils.removeEnd(sql, ", ") +
                " where id = '" + fxqhAssessReportTemplate.getId() + "' and del_flag = '0'";
        jdbcTemplate.update(sql);
    }
}
